# -*- coding: utf-8 -*-
"""
Created on Thu Mar 28 09:55:58 2024

@author: heyuesong
"""

import requests
import pandas as pd
from utils import general
import execjs
from lxml import etree

# js = general.read_js(r'D:\code\猿人学\第11题.js')


result_list = []
headers = {
    "authority": "www.python-spider.com",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "accept-language": "zh-CN,zh;q=0.9",
    "cache-control": "no-cache",
    "pragma": "no-cache",
    "sec-ch-ua": "\"Chromium\";v=\"122\", \"Not(A:Brand\";v=\"24\", \"Google Chrome\";v=\"122\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "none",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
}
cookies = {
    "sessionid": "1uwq6mztqzqy75b9oinixyip9w2u1v4d",
    "Hm_lvt_337e99a01a907a08d00bed4a1a52e35d": "1711520242,1711520884",
    "Hm_lpvt_337e99a01a907a08d00bed4a1a52e35d": "1711590597",
    "yuanrenxue34": "gVDC4P21in"
}
url = "https://www.python-spider.com/challenge/34"
response = requests.get(url, headers=headers, cookies=cookies)

cookies['yuanrenxue34'] = response.cookies.get_dict()['yuanrenxue34']

jss = general.requests_re('(var[\s\S]*?)</script>', response.text) + 'document.cookie="yuanrenxue34=' + response.cookies.get_dict()['yuanrenxue34'] + "\""


with open(r'D:\code\猿人学\第34题.js','r') as f:
    js = f.read().replace('jscode',jss)

cookies['iloveu'] = execjs.compile(js).call('sEnc')


response = requests.get(url, headers=headers, cookies=cookies)
html = etree.HTML(response.text)
l = sum([int(x.replace(' ','').replace('\n','')) for x in html.xpath('//td[@class="info"]/text()')])
